/*
 * @FileName: app_analog.c
 * @Author: GreyQiu qiushaogui@aikosolar.com
 * @Date: 2023-07-18 11:50:27
 * @LastEditors: GreyQiu qiushaogui@aikosolar.com
 * @LastEditTime: 2023-09-27 09:12:17
 * @Description: 
 * 
 * Copyright (c) 2023, All Rights Reserved. 
 */
#include "bsp.h"
#include "app_analog.h"
#include "app_calibration.h"
#include "app_fsm.h"

float g_mos_ftemp = 25.0f;
float g_mcu_ftemp = 25.0f;
int g_mos_temp = 25;
int g_mcu_temp = 25;

float g_pin = 0.0f; // 输入功率
float g_wh = 0.0f; // 累计发电量

PowerMeter Vrms;

/**
 * @description: 模拟信号单元表
 */
analog_t analog_tbl[ANALOG_ID_NUM] =
{
    {.id = ANALOG_ID_VAC, .k = COFF_K_VAC,  .b = COFF_B_VAC,  .Fc = 1000.0f, .Ts = 10e-6f, .k1 = 1.0f, .b1 = 0.0f}, // vin
};

/**
 * @description: 模拟信号初始化
 * 注意：大部分初始化都在模拟信号单元表中对个单元结构体成员赋值完成。
 * 这里仅对一阶低通滤波器做配置。
 * @return 无
 */
void app_analog_init(void)
{
    for (uint16_t id = 0; id < ANALOG_ID_NUM; id++) {
        easy_lpf_config_f32(&(analog_tbl[id].lpf), analog_tbl[id].Fc, analog_tbl[id].Ts);
        analog_tbl[id].real = 0;
        analog_tbl[id].real_flt = 0;
    }

    powerMeterConfig(&Vrms, 100e3, 1.0f, 55.0f, 45.0f);
}

unsigned int NtcTab[176] = 
{
    4087,4086,4085,4084,4084,4083,4081,4080,4079,4078,
    4076,4075,4073,4072,4070,4068,4066,4063,4061,4059,
    4056,4053,4050,4047,4044,4040,4037,4033,4029,4024,
    4020,4015,4010,4004,3998,3992,3986,3980,3973,3965,
    3958,3950,3941,3933,3923,3914,3904,3893,3882,3871,
    3859,3847,3834,3821,3807,3792,3777,3762,3745,3729,
    3711,3694,3675,3656,3636,3616,3595,3573,3551,3528,
    3505,3480,3456,3430,3404,3377,3350,3322,3294,3265,
    3235,3205,3174,3142,3111,3078,3045,3012,2978,2944,
    2909,2874,2839,2803,2767,2731,2695,2658,2621,2584,
    2546,2509,2471,2434,2396,2359,2321,2283,2246,2209,
    2171,2134,2097,2060,2024,1987,1951,1915,1880,1845,
    1810,1775,1741,1707,1674,1641,1608,1576,1544,1512,
    1482,1451,1421,1391,1362,1334,1306,1278,1251,1224,
    1198,1172,1147,1122,1097,1073,1050,1027,1004,982,
    961,940,919,899,879,859,840,822,803,786,
    768,751,735,718,702,687,672,657,642,628,
    614,601,588,575,562,550
};


/**
 * @description: 温度查表算法
 * @param {unsigned int} volt
 * @return {*}
 */
int Tempera_cal( unsigned int volt)
{
    int i;
    for(i=0;i<176;i++)
    {
        if(NtcTab[i] <= volt)
            break;
        else if(NtcTab[175] >= volt)
        {
            i=175;
        }
    }
    
    return(i - 50);//
}

// /**
//  * @description: 温度公式算法
//  * @param {float} Rntc
//  * @return {*}
//  */
// float temperature_cal(float Rntc)
// {
//     float B = 4050.0f;
//     float T25 = 298.15f;
//     float R25 = 4700.0f;
//     volatile float N1, N2, N3, N4;
//     N1 = log(Rntc / R25) / B;
//     N2 = N1 + 1 / T25;
//     N3 = 1 / N2;
//     N4 = N3 - 237.15f;
//     return N4;
// }



void app_analog_task(void)
{

}


